package com.item.action;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.base.Page;
import com.base.dialect.PaginationSupport;
import com.base.util.JSONUtils;
import com.base.util.StringUtil;
import com.item.dao.model.Admin;
import com.item.dao.model.AdminExample;
import com.item.dao.model.School;
import com.item.daoEx.model.AdminEx;
import com.item.exception.JSONRepUtils;
import com.item.service.AdminService;
import com.item.service.SchoolService;
import com.mdx.mobile.commons.verfy.Md5;

@Controller
public class AdminController {
	@Autowired
	private AdminService adminService;
	@Autowired
	private SchoolService schoolService;
	
	@RequestMapping("/listAdmin")
	@ResponseBody
	public String list(Page param, Integer isNotPage, Integer isM) throws Exception{
		Object objForSer = null;
//		AdminExample example = new AdminExample();
		PaginationSupport.setContext(param.getPageStart(), param.getPageSize(), null);

		List<AdminEx> list = adminService.selectAdminEx();
	
		if(isNotPage != null && isNotPage == 1) {
			objForSer = list;
		} else {
			objForSer = new Page(PaginationSupport.getContext().getTotalCount(), list);
		}
		return JSONRepUtils.serialize(objForSer, isM);
	}
	
	@RequestMapping("/showlistAdmin")
	public String list() {
		return "admin/adminList";
	}
	
	@RequestMapping("/toEditAdmin")
	public String toEdit()throws Exception{
		return "admin/adminEdit";
	}
	
	@RequestMapping(value="/saveAdmin",method=RequestMethod.POST)
	@ResponseBody
	public String save(Admin admin, Integer isTop, HttpSession session)throws Exception{		
		String sName = "";
		if(StringUtils.isNotBlank(admin.getAppid())) {
			School s = schoolService.selectByPrimaryKey(admin.getAppid());
			if(s != null) {
				sName = s.getName();
			}
		}
		try{
			if(StringUtil.isEmpty(admin.getId())){
				admin.setPassword(Md5.md5(admin.getPassword()));
				admin.setCreateTime(new Date());
				admin.setState(1);
				admin.setsName(sName);
				if(sName.equals("所有")) {
					admin.setAppid("all");
					admin.setsName("系统管理员");
				}
				adminService.insertSelective(admin);
			} else {
				admin.setModifyTime(new Date());
				adminService.updateByPrimaryKeySelective(admin);
			}
		}catch(Exception e){
			e.printStackTrace();
			return "{\"success\":false}";
		}
		
		return "{\"success\":true}";
	}
	
	@RequestMapping(value="/findByIdAdmin")
	@ResponseBody
	public String findById(String id,HttpSession session)throws Exception{		
		Admin n = adminService.selectByPrimaryKey(id);

		return JSONUtils.serialize(n);
	}
	
	@RequestMapping("/delAdmin")
	@ResponseBody
	public String del(String id,String url,HttpSession session)throws Exception{
		String[] ids = id.split(",");
		int len = ids.length;
		for(int i = 0; i < len; i++) {
			adminService.deleteByPrimaryKey(ids[i]);
		}
		return "{\"success\":true}";
	}
	
	
}

